#include "dkglTimer.h"

namespace DKGL
{
	Timer GlobalTimer;

	MillisecondTime MillisecondTime::current()
	{
		static class Cached
		{
			LONGLONG m_frequency;
			LONGLONG m_base;
		public:
			Cached()
			{
				QueryPerformanceFrequency((LARGE_INTEGER *) &m_frequency);
				QueryPerformanceCounter((LARGE_INTEGER *) &m_base);
			}
			LONGLONG frequency()
			{
				return m_frequency;
			}
			LONGLONG base()
			{
				return m_base;
			}
		} cached;
    
		if(cached.frequency() > 0)
		{
			LONGLONG count;
			QueryPerformanceCounter((LARGE_INTEGER *) &count);
			return time_from_ticks(count - cached.base(), cached.frequency());
		}
		else
		{
			return MillisecondTime();
		}
	}
}

ClockWatch::ClockWatch()
{
	QueryPerformanceFrequency(&m_freq);
}

ClockWatch::~ClockWatch()
{}

void ClockWatch::start()
{
	QueryPerformanceCounter(&m_startTime);
}

double ClockWatch::stop()
{
	QueryPerformanceCounter(&m_endTime);     
	return static_cast<double>(m_endTime.QuadPart- m_startTime.QuadPart)/ static_cast<double>(m_freq.QuadPart);
}

}